﻿if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fd_Portada_get_by_Expedienteid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[fd_Portada_get_by_Expedienteid]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE fd_Portada_get_by_Expedienteid
(
	@id	int
)
AS

BEGIN

	SET NOCOUNT ON
	
		--Declare local variable
				
   	BEGIN TRY
   		IF exists (select NoExpediente from Expedientes where id = @id)
   			BEGIN
   				select distinct p.* from Portadas p, expedientes e where e.id = @id and p.NoExpediente = e.NoExpediente
   			END
   		ELSE
   			BEGIN
   				RAISERROR ('El Expediente no contiene Portada', 0, 1);
   			END   		
   		
   	END TRY
   	
   	BEGIN CATCH
   		DECLARE 	@ErrorNumber	INT,
				@ErrorSeverity	INT,
				@ErrorState		INT,
				@ErrorProcedure NVARCHAR(126),
				@ErrorLine		INT,
				@ErrorMessage	NVARCHAR(4000)
				
   		SELECT		@ErrorNumber = ERROR_NUMBER(),
				@ErrorSeverity = ERROR_SEVERITY(),
				@ErrorState = ERROR_STATE(),
				@ErrorProcedure = ERROR_PROCEDURE(),
				@ErrorLine = ERROR_LINE(),
				@ErrorMessage = ERROR_MESSAGE()
				
   		RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)
   	END CATCH
END

